#
# Cookbook Name:: newrelic
# Recipe:: install
#
# Copyright 2013, IBM
#
# All rights reserved - Do Not Redistribute
#

cuser = node[:newrelic][:user]
cgroup = node[:newrelic][:group]
db2user = node[:newrelic][:db2_user]
db2group = node[:newrelic][:db2_group]
deployment = node[:deployment]

startinstr "Install newrelic for db2"

cookbook_file "#{node[:newrelic][:ksh_path]}" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "ksh"
  action :create
end

cookbook_file "#{node[:newrelic][:kshman_path]}" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "ksh-man"
  action :create
end

cookbook_file "#{node[:newrelic][:newrelicksh_path]}" do
  backup false
  owner db2user
  group db2group
  mode 0755
  source "newrelic.ksh"
  action :create
end

bash "Update newrelic" do
  user cuser
  group cgroup
  cwd "#{node[:newrelic][:newrelicksh_dir]}"
  code <<-EOH
    sudo sed -i 's/DATABASE=omdb/DATABASE=#{node[:om][:omdbname]}/g' #{node[:newrelic][:newrelicksh_path]}
    sudo sed -i 's/proxyhost=10.61.99.95/proxyhost=#{node[:newrelic][:proxyhost]}/g' #{node[:newrelic][:newrelicksh_path]}
    sudo sed -i 's/proxyport=3128/proxyport=#{node[:newrelic][:proxyport]}/g' #{node[:newrelic][:newrelicksh_path]}
  EOH
end

bash "Startup newrelic" do
  user cuser
  group cgroup
  cwd "/home/db2inst1"
  code <<-EOH
    su - db2inst1 -c "/usr/bin/nohup /home/db2inst1/newrelic.ksh 2>/dev/null > /dev/null &"
  EOH
end

stopinstr "Install newrelic for db2"
